﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using DevExpress.Utils;

public partial class UserControl_ControlReportThongTinSanPham : System.Web.UI.UserControl
{
    ClassExecute.ClassExecute dt;
    string str = webapp4U.Configuration.CurrentConnectionString;
    string error = "";
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            if (Session["db"] != null)
            {
                DataTable db = (DataTable)Session["db"];
                ASPxPivotGrid1.DataSource = db;
                ASPxPivotGrid1.DataBind();
            }
        }
    }
    protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            DataTable db = dt.Get_Data_DataTable(str, ref error, "[ls_getItemInfo]", CommandType.StoredProcedure);
            if (db.Rows.Count > 0)
            {

                ASPxPivotGrid1.DataSource = db;
                Session["db"] = db;

            }
            else
            {
                if (Session["db"] != null)
                {
                    Session["db"] = null;
                }
                ASPxPivotGrid1.DataSource = null;
            }
            ASPxPivotGrid1.DataBind();
        }
        catch (Exception ex)
        {
           
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }   
    }
    protected void imgBtnSave_Click(object sender, ImageClickEventArgs e)
    {
        using (MemoryStream filestream = new MemoryStream())
        {
            ASPxPivotGridExporter1.OptionsPrint.PrintFilterHeaders = DefaultBoolean.False;
            ASPxPivotGridExporter1.OptionsPrint.PrintColumnHeaders = DefaultBoolean.True;
            ASPxPivotGridExporter1.OptionsPrint.PrintRowHeaders = DefaultBoolean.True;
            ASPxPivotGridExporter1.OptionsPrint.PrintDataHeaders = DefaultBoolean.False;

            string contentType = "application/ms-excel";
            string fileName = "BaoCaoThongTinSanPham" + DateTime.Now.ToString("yyyyMMdd") + ".xls";
            ASPxPivotGridExporter1.ExportToXls(filestream);
            byte[] buffer = filestream.GetBuffer();
            Response.Clear();
            Response.Buffer = false;
            Response.AppendHeader("Content-Type", contentType);
            Response.AppendHeader("Content-Transfer-Encoding", "binary");
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
            Response.BinaryWrite(buffer);
            Response.End();
        }
    }
    protected void imgBtnRefresh_Click(object sender, ImageClickEventArgs e)
    {
        for (int i = 0; i < ASPxPivotGrid1.Fields.Count; i++)
        {
            ASPxPivotGrid1.Fields[i].FilterValues.Clear();
        }
    }
}